package com.ler.yrmall.member.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ler.yrmall.basic.Form;
import com.ler.yrmall.exception.BusinessException;
import com.ler.yrmall.member.entity.GrowthChangeHistoryEntity;
import com.ler.yrmall.member.entity.IntegrationChangeHistoryEntity;
import com.ler.yrmall.member.entity.MemberEntity;
import com.ler.yrmall.member.mapper.MemberMapper;
import com.ler.yrmall.member.service.GrowthChangeHistoryService;
import com.ler.yrmall.member.service.IntegrationChangeHistoryService;
import com.ler.yrmall.member.service.MemberService;
import com.ler.yrmall.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author leron
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberEntity> implements MemberService {

    @Resource
    private GrowthChangeHistoryService growthChangeHistoryService;

    @Resource
    private IntegrationChangeHistoryService integrationChangeHistoryService;

    @Override
    public IPage<MemberEntity> find(Form request) {
        return this.page(request.getPage(),Wrappers.<MemberEntity>lambdaQuery()
                .eq(StringUtils.isNotBlank(request.getKeyword()),MemberEntity::getUsername,request.getKeyword()));
    }

//    @Override
//    public void register(UserRegisterVO userRegisterVO) throws PhoneExistException, UsernameExistException {
//        MemberEntity memberEntity = new MemberEntity();
//        usernameCheck(userRegisterVO.getUsername());
//        phoneCheck(userRegisterVO.getPhone());
//        memberEntity.setMobile(userRegisterVO.getPhone());
//        memberEntity.setUsername(userRegisterVO.getUsername());
//        memberEntity.setCreateTime(LocalDateTime.now());
//        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//        memberEntity.setPassword(passwordEncoder.encode(userRegisterVO.getPassword()));
//        this.save(memberEntity);
//    }

    @Override
    public MemberEntity getMemberByUserName(String username) {
        return this.getOne(Wrappers.<MemberEntity>lambdaQuery().eq(MemberEntity::getUsername,username));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String checkin() {
        long count = growthChangeHistoryService.count(Wrappers.<GrowthChangeHistoryEntity>lambdaQuery()
                .eq(GrowthChangeHistoryEntity::getDataSource, GrowthChangeHistoryEntity.DataSource.CHECKIN.getCode())
                .apply("DATE_FORMAT(create_time, '%Y-%m-%d') = {0}", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))));
        if (count != 0) {
            throw new BusinessException("您今天已经签到过了");
        }
        growthChangeHistoryService.checkin();
        integrationChangeHistoryService.checkin();
        MemberEntity member = this.getById(SecurityUtils.getLoginUser().getId());
        // todo mysql for update
        member.setIntegral(member.getIntegral() + IntegrationChangeHistoryEntity.DataSource.CHECKIN.getScore());
        member.setGrowth(member.getGrowth() + GrowthChangeHistoryEntity.DataSource.CHECKIN.getScore());
        member.updateById();
        return "签到成功,获得成长值" + GrowthChangeHistoryEntity.DataSource.CHECKIN.getScore() + ",获得积分" + IntegrationChangeHistoryEntity.DataSource.CHECKIN.getScore();
    }
}